package siga.visao_busca;

import java.sql.SQLException;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;


public class buscaDisciplina extends javax.swing.JDialog {

    public static String bd_curso="";

    public buscaDisciplina(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfCurso = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfDisciplina = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDisciplina = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscando Disciplina");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Curso:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCurso.setName("jtfCurso"); // NOI18N
        jtfCurso.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCursoKeyPressed(evt);
            }
        });

        jLabel2.setText("Disciplina:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfDisciplina.setName("jtfDisciplina"); // NOI18N
        jtfDisciplina.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDisciplinaKeyPressed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jButton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtDisciplina.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Curso", "Fase", "Código", "Disciplina"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtDisciplina.setName("jtDisciplina"); // NOI18N
        jtDisciplina.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtDisciplinaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtDisciplina);
        jtDisciplina.getColumnModel().getColumn(0).setPreferredWidth(120);
        jtDisciplina.getColumnModel().getColumn(0).setMaxWidth(120);
        jtDisciplina.getColumnModel().getColumn(1).setMinWidth(60);
        jtDisciplina.getColumnModel().getColumn(1).setPreferredWidth(60);
        jtDisciplina.getColumnModel().getColumn(1).setMaxWidth(60);
        jtDisciplina.getColumnModel().getColumn(2).setPreferredWidth(70);
        jtDisciplina.getColumnModel().getColumn(2).setMaxWidth(70);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtfDisciplina, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        busca();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtDisciplinaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDisciplinaMouseClicked
        if(evt.getClickCount()==2){
            bd_curso = jtDisciplina.getValueAt(jtDisciplina.getSelectedRow(), 0).toString();
            index.LRdisciplina.add(0,jtDisciplina.getValueAt(jtDisciplina.getSelectedRow(), 2).toString());
            index.LRdisciplina.add(1,jtDisciplina.getValueAt(jtDisciplina.getSelectedRow(), 3).toString());
            dispose();
        }
    }//GEN-LAST:event_jtDisciplinaMouseClicked

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        if(!bd_curso.isEmpty()){
           jtfCurso.setText(bd_curso.substring(0,3));
        }
    }//GEN-LAST:event_formWindowOpened

    private void jtfCursoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCursoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfCursoKeyPressed

    private void jtfDisciplinaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDisciplinaKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfDisciplinaKeyPressed

    private void busca(){
        try {
            String curso;
            String disciplina;
            if (jtfCurso.getText().equals("")) {curso = "";} else {curso = " and gd.cod_curso=" + jtfCurso.getText();}
            if (jtfDisciplina.getText().equals("")) {disciplina = "";} else {disciplina = " and gd.descricao like \"" + jtfDisciplina.getText() + "%\"";}
            conexao.Query("select gd.*, gc.cod_fase from grade_curricular gc "
                    + " inner join grade_disciplina gd on gd.codigo=gc.cod_disciplina and gd.cod_curso=gc.cod_curso and gd.cod_emp=gc.cod_emp "
                    + " where gd.cod_emp=" + index.cod_emp + curso + " " + disciplina + " order by gc.ordem");
            javax.swing.table.DefaultTableModel jtd = (javax.swing.table.DefaultTableModel) jtDisciplina.getModel();
            for (int x = jtDisciplina.getRowCount() - 1; x >= 0; --x) {
                jtd.removeRow(x);
            }
            while (conexao.rs.next()) {
                jtd.addRow(new Object[]{conexao.rs.getString("cod_curso"), conexao.rs.getString("cod_fase"),conexao.rs.getString("codigo"), conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtDisciplina;
    private javax.swing.JTextField jtfCurso;
    private javax.swing.JTextField jtfDisciplina;
    // End of variables declaration//GEN-END:variables

}
